package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FuturesAccountEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FuturesAccountDetailProcessor implements PageProcessor {

	private static Logger logger = Logger.getLogger(FuturesAccountDetailProcessor.class);
	
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	private static final String BASE_URL = "http://ba.amac.org.cn/pages/amacWeb/qh-web-view.html";
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue,
			List<Proccessable> objectContainer) throws Exception {
		
		JSONArray jsonArr = JSONArray.fromObject(page.getContent());
		JSONObject jsonObj = jsonArr.getJSONObject(0);
		
		PageRequest request = page.getRequest();
		JSONObject retObj = (JSONObject)request.getExtParams().get("retObj");
		String pageIndex = (String)request.getExtParams().get("pageIndex");
		int count = atomicInteger.incrementAndGet();
		
		logger.info("当前第"+pageIndex+"页,正在解析第"+count+"条数据:当前期货产品{"+jsonObj.getString("MPI_PRODUCT_CODE")+":"+jsonObj.getString("AOI_NAME")+"}");
		
		FuturesAccountEntity fae = new FuturesAccountEntity();
		fae.setCreateTime(new Timestamp(System.currentTimeMillis()));
		fae.setUrl(BASE_URL+"?id="+retObj.getString("MPI_ID"));
		fae.setAoiName(jsonObj.getString("AOI_NAME"));
		fae.setMpiCreateDate(DateUtils.parseDate(jsonObj.getString("MPI_CREATE_DATE"), new String[]{"yyyy-MM-dd"}));
		fae.setMpiGroupRatio(jsonObj.getString("MPI_GROUP_RATIO"));
		fae.setMpiId(retObj.getString("MPI_ID"));
		fae.setMpiName(retObj.getString("MPI_NAME"));
		fae.setMpiParticipationUser(jsonObj.getInt("MPI_PARTICIPATION_USER"));
		fae.setMpiProductCode(jsonObj.getString("MPI_PRODUCT_CODE"));
		fae.setMpiTotalMoney(jsonObj.getDouble("MPI_TOTAL_MONEY"));
		fae.setMpiTrustee(jsonObj.getString("MPI_TRUSTEE"));
		fae.setRn(retObj.getInt("RN"));
		fae.setSfjgh(jsonObj.getString("SFJGH"));
		fae.setTzlx(jsonObj.getString("TZLX"));
		fae.setId(UuIdPublic.timeStrongID(5));
		StringUtils.removeSpace(fae);
		objectContainer.add(fae);
		
	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
